﻿using NAnoto;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MicroTest
{
    public partial class TeacherViewer : Form
    {
        public TeacherViewer()
        {
            InitializeComponent();
            panel3.BackColor = Color.FromArgb(234, 246, 248);
            panel4.BackColor = Color.FromArgb(208, 214, 226);
            label1.ForeColor = Color.Red;
            label2.ForeColor = Color.FromArgb(0, 0, 0);
            pictureBox2.BackColor = Color.FromArgb(2, 178, 201);
            pictureBox3.BackColor = Color.FromArgb(2, 178, 201);

        }

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
        }
        private SYMInfo SymInfo = null;
        private void TeacherViewer_Load(object sender, EventArgs e)
        {
            class2.Visible = false;
            class1.Visible = true;
            studentListDic = new Dictionary<string, string>();
            for (int i = 1; i < 20; i++)
            {
                studentListDic.Add(i.ToString(), "学生-" + i.ToString());
            }
            statusWindows = new Dictionary<string, DrawingWindow>();

            SYMTOAFD sym2afd = new SYMTOAFD();
            SymInfo = sym2afd.UNZIPSYM(@"D:\ChromeDownload\2017120002.sym");
            Program.CurrentClassInfo.ClearSymPageImage();
            foreach (var item in SymInfo.lstSYMPNG)
            {
                Image img = Image.FromFile(item.Value.PngFile);
                Bitmap bitmap = new Bitmap(img, new Size(img.Width / 2, img.Height / 2));
                img.Dispose();
                Program.CurrentClassInfo.AddSymPageImage(item.Value.PageAddress, bitmap);
            }
            LoadDrawingWindws();

        }
        bool doubleclick = false;
        private void class1_main_Click(object sender, EventArgs e)
        {
            class2.Visible = false;
            class1.Visible = true;
            label1.ForeColor = Color.Red;
            label2.ForeColor = Color.FromArgb(0, 0, 0);
            class1_main.BackColor = Color.FromArgb(234, 246, 248);
        }

        private void class2_main_Click(object sender, EventArgs e)
        {
            class2.Visible = true;
            label2.ForeColor = Color.Red;
            label1.ForeColor = Color.FromArgb(0, 0, 0);
            class1.Visible = false;

        }
        private Dictionary<string, string> studentListDic;
        private Dictionary<string, DrawingWindow> statusWindows;
        public bool ResumeMode = false;
        private int windowWidth = 0;
        private int windowHeight = 0;
        private const int minDrawingWidth = 210;
        private const int minDrawingHeight = 297;
        private const int drawingWindowNameHeight = 35;
        private List<ZoomSize> zoomValues = new List<ZoomSize>();
        private void LoadDrawingWindws()
        {
            bool isA4Page = true;
            foreach (var stu in studentListDic)
            {
                string penSerial = stu.Key;
                DrawingWindow cs = new DrawingWindow(penSerial, Program.CurrentClassInfo.ClassSymImage, ResumeMode);
                //cs.SuspendLayout();
                cs.SetName(stu.Value);
                statusWindows.Add(penSerial, cs);
                int width = class1.Width;
                if (windowHeight != 0)
                {
                    cs.Height = windowHeight;
                    cs.Width = windowWidth;
                }
                else
                {
                    if (Program.CurrentClassInfo.ClassSymImage.Count != 0)
                    {
                        foreach (var item in Program.CurrentClassInfo.ClassSymImage)
                        {
                            if (item.Value.Width < item.Value.Height)
                            {
                                windowWidth = minDrawingWidth;
                                windowHeight = minDrawingHeight + drawingWindowNameHeight;
                            }
                            else
                            {
                                isA4Page = false;
                                windowWidth = 2 * minDrawingWidth;
                                windowHeight = minDrawingHeight + drawingWindowNameHeight;
                            }
                            break;
                        }
                        cs.Width = windowWidth;
                        cs.Height = windowHeight;
                        //cs.SetWindowsSize(windowWidth, windowHeight);

                    }
                }
                int w = cs.Width;
                int l = width % w / 2;
                cs.Margin = new Padding(20, 10, 20, 10);
                cs.Visible = true;
                try
                {

                    Label ll = (Label)(cs.Controls.Find("labPenID", true)[0]);
                    ll.DoubleClick += new EventHandler(Cs_Click);
                    PictureBox pb = (PictureBox)(cs.Controls.Find("picDraw", true)[0]);
                    pb.DoubleClick += new EventHandler(Cs_Click);
                    class1.Controls.Add(cs);
                }
                catch (Exception ee)
                {

                }

            }

            zoomValues.Clear();
            int smallWidth = minDrawingWidth;
            int smallHeight = minDrawingHeight;
            if (!isA4Page)
                smallWidth = 2 * minDrawingWidth;

            ZoomSize sz = new ZoomSize(smallWidth, smallHeight + drawingWindowNameHeight);
            zoomValues.Add(sz);

            int clientAreaHeight = class1.Height - 10;
            int clientAreaWidth = class1.Width - 20 - 20;

            int drawingWindowHeight = clientAreaHeight;
            int drawingWindowWidth = (int)(1.0 * (drawingWindowHeight - drawingWindowNameHeight) * smallWidth / smallHeight);
            sz = new ZoomSize(drawingWindowWidth, drawingWindowHeight);
            zoomValues.Add(sz);


            drawingWindowWidth = clientAreaWidth;
            drawingWindowHeight = (int)(1.0 * drawingWindowWidth / smallWidth * smallHeight) + drawingWindowNameHeight;
            sz = new ZoomSize(drawingWindowWidth, drawingWindowHeight);
            zoomValues.Add(sz);
            int num = 0;
            foreach (var item in class1.Controls)
            {
                (item as DrawingWindow).SetLabelVisable(true);
            }
        }

        private void Cs_Click(object sender, EventArgs e)
        {
            if (!doubleclick)
            {
                Label ll = (Label)(sender);
                int num = int.Parse(ll.Text.Split('-')[1]);
                if (class1.Controls.Count == 0)
                    return;
                int controlCount = class1.Controls.Count;
                Control numc = class1.Controls[num - 1];
                class1.Controls.Clear();
                class1.Controls.Add(numc);
                foreach (var item in statusWindows)
                {
                    int width = item.Value.Width;
                    int height = item.Value.Height;
                    int index = -1;
                    for (int i = 0; i < zoomValues.Count; i++)
                    {
                        if (zoomValues[i].X == width && zoomValues[i].Y == height)
                        {
                            index = i;
                            break;
                        }
                    }
                    if (index == zoomValues.Count - 1 || index == -1)
                        break;
                    index += 1;
                    ZoomSize zs = zoomValues[index];

                    width = zs.X;
                    height = zs.Y;
                    item.Value.SuspendLayout();
                    item.Value.Width = width;
                    item.Value.Height = height;
                    int leftMargin = 0;
                    if (index == 1)
                        leftMargin = (class1.Width - width) / 2 - 10;
                    else if (index == 2)
                        leftMargin = 10;
                    item.Value.Margin = new Padding(leftMargin, 10, leftMargin, 10);
                    item.Value.ResumeLayout();
                    windowWidth = width;
                    windowHeight = height;
                }
                doubleclick = true;
            }
            else
            {
                if (class1.Controls.Count == 0)
                    return;
                int controlCount = class1.Controls.Count;

                int width = class1.Controls[0].Width;
                int height = class1.Controls[0].Height;
                int index = -1;
                for (int i = 0; i < zoomValues.Count; i++)
                {
                    if (zoomValues[i].X == width && zoomValues[i].Y == height)
                    {
                        index = i;
                        break;
                    }
                }
                if (index == 0 || index == -1)
                    return;
                index -= 1;
                class1.SuspendLayout();
                if (index == 0 && statusWindows.Count > 1)
                    class1.Controls.Clear();


                foreach (var item in statusWindows)
                {
                    ZoomSize zs = zoomValues[index];

                    width = zs.X;
                    height = zs.Y;
                    item.Value.SuspendLayout();
                    item.Value.Width = width;
                    item.Value.Height = height;
                    int leftMargin = 0;
                    if (index == 0)
                        leftMargin = 20;
                    else if (index == 1)
                        leftMargin = (class1.Width - width) / 2 - 10;
                    else if (index == 2)
                        leftMargin = 10;
                    item.Value.Margin = new Padding(leftMargin, 10, leftMargin, 10);
                    item.Value.ResumeLayout();
                    windowWidth = width;
                    windowHeight = height;
                    if (index == 0 && statusWindows.Count > 1)
                    {
                        item.Value.SetLabelVisable(false);
                        //item.Value.Visible = false;
                        class1.Controls.Add(item.Value);

                    }
                }
                class1.ResumeLayout();
                foreach (var item in class1.Controls)
                {
                    (item as DrawingWindow).SetLabelVisable(true);
                }
                doubleclick = false;

            }

        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void btnExt_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void panel4_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void panel4_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(DrawingWindow)))
            {
                DrawingWindow item = (DrawingWindow)e.Data.GetData(typeof(DrawingWindow));
                if (e.Effect == DragDropEffects.Copy)
                {
                    panel4.Controls.Add(item);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var item = panel4.Controls;
            //panel4.Controls.Clear();
            StudentView sv = new StudentView(item);
            sv.FormClosed += Sv_FormClosed;         
            sv.ShowDialog();
        }
        private void Sv_FormClosed(object sender, FormClosedEventArgs e)
        {
            StudentView sv = (StudentView)(sender);
            var studpanl = (FlowLayoutPanel)(sv.Controls.Find("studentpanel", true)[0]);
            while (studpanl.Controls.Count > 0)
            {
                foreach (Control c in studpanl.Controls)
                {
                    class1.Controls.Add(c as DrawingWindow);
                    studpanl.Controls.Remove(c);
                }
            }

        }
        private void button3_Click(object sender, EventArgs e)
        {
            while(panel4.Controls.Count>0)
            {
                foreach (Control c in panel4.Controls)
                {
                    class1.Controls.Add(c as DrawingWindow);
                    panel4.Controls.Remove(c);
                }

            }

        }

        private void vote_Click(object sender, EventArgs e)
        {
            FrmVote fv = new FrmVote(studentListDic);
            fv.ShowDialog();
        }
    }
}
